package com.derbtec.refale.activities;

import java.text.ParseException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;

import com.derbtec.refale.R;
import com.derbtec.refale.models.Participant;
import com.derbtec.refale.util.HelperUtils;

public class EventParticipantsListViewActivity extends ListActivity {

	private ArrayList<Participant> participants = new ArrayList<Participant>();
	private EventParticipantsListViewAdapter adapter;
	private String jsonstring;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        Bundle extras = getIntent().getExtras();
        setJsonstring(extras.getString("jsonstring"));
		
		if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
		{
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
		
		
		try
		{
        JSONArray jArray = new JSONArray(jsonstring);
  
        for(int i=0;i<jArray.length();i++){
        	
            JSONObject json_data 	= jArray.getJSONObject(i);
            Participant participantbuffer = HelperUtils.ParticipantFromJson(json_data, new Participant());
            participants.add(participantbuffer);

            
            
        }
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setContentView(R.layout.activity_participant_list_view);

		adapter = new EventParticipantsListViewAdapter(this, participants);
		setListAdapter(adapter);
		
	}

	public void updateData() {
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		

	}


	public ArrayList<Participant> getElements() {
		return participants;
	}

	public void setElements(ArrayList<Participant> elements) {
		this.participants = elements;
	}
	
	public void addElements(ArrayList<Participant> elements) {
		this.participants.addAll(elements);
	}

	public String getJsonstring() {
		return jsonstring;
	}

	public void setJsonstring(String jsonstring) {
		this.jsonstring = jsonstring;
	}


}
